package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.查询当前购物车有没有这件商品
        ShoppingCart shoppingCartCondition = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCartCondition);
        shoppingCartCondition.setUserId(BaseContext.getCurrentId());
        //查询购物车
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectById(shoppingCartCondition);
        //2.如果查询到的话则商品数量+1
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
            //有的话数量加一 下面添加不执行
            return;
        }
        //如果未查询到购物车里有当前菜品 判断传入的的是菜品id还是套餐id
        if (shoppingCartDTO.getDishId() != null) {
            Dish dish = dishMapper.getDishByid(shoppingCartDTO.getDishId());
            shoppingCartCondition.setName(dish.getName());
            shoppingCartCondition.setAmount(dish.getPrice());
            shoppingCartCondition.setImage(dish.getImage());
        } else {
            Setmeal setmeal = setmealMapper.getBySetmealId(shoppingCartDTO.getSetmealId());
            shoppingCartCondition.setName(setmeal.getName());
            shoppingCartCondition.setAmount(setmeal.getPrice());
            shoppingCartCondition.setImage(setmeal.getImage());
        }
        //3.没有查询到的话则添加商品到购物车当中
        //设置公共字段
        shoppingCartCondition.setNumber(1);
        shoppingCartCondition.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCartCondition);
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectById(shoppingCart);
        return shoppingCartList;
    }

    @Override
    public void deleteAll() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //先判断是不是最后一件商品 是最后一件商品直接清除 不是的话 数量nubmer减1
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectById(shoppingCart);
        //如果是最后一件商品 则删除对应的购物车数据
         if (shoppingCartList.get(0).getNumber() == 1){
             shoppingCartMapper.delete(shoppingCartList.get(0).getId());
        }else{
             //如果不是最后一件商品 则数量减一
             ShoppingCart shoppingCart1 = shoppingCartList.get(0);
             shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
             shoppingCartMapper.update(shoppingCart1);
         }

    }
}
